package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.AddressBook;
import com.itheima.reggie.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Description: // 类说明，在创建类时要填写
 * @ClassName: AddressBookController    // 类名，会自动填充
 * @Author: MYH          // 创建者
 * @Date: 2022/12/20 7:52   // 时间
 * @Version: 1.0     // 版本
 */
@RestController
@RequestMapping("/addressBook")
@Slf4j
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;


    /**
     * 新增地址
     * @param addressBook
     * @return
     */
    @PostMapping
    public R<String> saveAddr(@RequestBody AddressBook addressBook ,HttpSession session){

        //从session中获取用户id
        Long user = (Long)session.getAttribute("user");
        addressBook.setUserId(user);

        //将地址信息保存到数据库当中
        addressBookService.save(addressBook);
        return R.success("保存成功！！");
    }

    /**
     * 查询指定用户的地址消息l
     * @return
     */
    @GetMapping("/list")
    public R<List<AddressBook>> getAddr(HttpSession session){

        Long user = (Long)session.getAttribute("user");
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(user != null,AddressBook::getUserId,user);
        queryWrapper.orderByDesc(AddressBook::getUpdateTime);
        List<AddressBook> list = addressBookService.list(queryWrapper);
        return R.success(list);
    }

    /**
     * 设置地址默认值
     * @param addressBook
     * @return
     */
    @PutMapping("/default")
    public R<String> updateDef(@RequestBody AddressBook addressBook){

        log.info("前端传来的数据：{}",addressBook);

        //根据id来查询user_id
        Long addressBookId = addressBook.getId();
        AddressBook serviceById = addressBookService.getById(addressBookId);
        Long userId = serviceById.getUserId();

        //根据userId来查询所有的地址
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getUserId,userId);
        List<AddressBook> list = addressBookService.list(queryWrapper);

        //将集合当中的def值改均为0
        for (AddressBook book : list) {
            book.setIsDefault(0);
        }
        addressBookService.updateBatchById(list);

        //将所选的addr改为默认值
        addressBook.setIsDefault(1);
        addressBookService.updateById(addressBook);

        return R.success("修改成功！！");
    }


    /**
     * 修改地址时的回显操作
     * @return
     */
    @GetMapping("/{id}")
    public R<AddressBook> reviewAddr(@PathVariable("id") Long id){

        AddressBook addressBook = addressBookService.getById(id);
        return R.success(addressBook);
    }

    /**
     * 修改地址
     * @param addressBook
     * @return
     */
    @PutMapping
    public R<String> updateAddr(@RequestBody AddressBook addressBook){

        addressBookService.updateById(addressBook);
        return R.success("修改地址成功！！");
    }

    /**
     * 删除地址
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> delAddr(Long ids){

        addressBookService.removeById(ids);
        return R.success("删除成功！！");
    }


    /**
     * 获取当前登录用户的默认地址
     * @return
     */
    @GetMapping("/default")
    public R<AddressBook> getDefault(HttpSession session){

        //获取当前登录用户的user_id
        Object user = session.getAttribute("user");
        Long userId = (Long) user;
        //根据user_id来获取默认地址
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getUserId,userId);
        queryWrapper.eq(AddressBook::getIsDefault,1);
        AddressBook one = addressBookService.getOne(queryWrapper);
        return R.success(one);
    }

}
